package com.hvisions.service.impl;

import com.hvisions.repository.PermissionRepository;
import com.hvisions.repository.RoleRepository;
import com.hvisions.entity.Permission;
import com.hvisions.entity.Role;
import com.hvisions.entity.RolePermission;
import com.hvisions.entity.User;
import com.hvisions.mapper.RolePermissionMapper;
import com.hvisions.mapper.UserMapper;
import com.hvisions.service.IRoleService;
import com.hvisions.util.CoreConst;
import com.hvisions.util.ResultUtil;
import com.hvisions.util.UUIDUtil;
import com.hvisions.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: Lau
 * @Description:
 * @Date: Created in 11:40 2019/10/24
 * @Modified By:
 */
@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    RoleRepository roleRepository;

    @Autowired
    UserMapper userMapper;

    @Autowired
    PermissionRepository permissionRepository;
    @Autowired
    RolePermissionMapper rolePermissionMapper;


    public Set<String> findRoleByUserId(String userId) {
        return roleRepository.findRoleByUserId(userId);
    }

    @Override
    public List<Role> selectRoles(Role role) {
       if(role.getName()==null||"".equals(role.getName())){
           return roleRepository.findAllByStatus(CoreConst.STATUS_VALID);
       }
        return roleRepository.findAllByNameLike("%"+role.getName()+"%");
    }

    @Override
    public void insert(Role role) {
        role.setRoleId(UUIDUtil.getUniqueIdByUUId());
        role.setStatus(1);
        role.setCreateTime(new Date());
        Role role1 = roleRepository.save(role);

    }

    @Override
    public int updateStatusBatch(List<String> roleIds, Integer status) {

        return roleRepository.updateStatusBatch(roleIds,status);
    }

    @Override
    public Role findById(Integer id) {

        return roleRepository.getOne(id);
    }

    @Override
    public int updateByRoleId(Role role) {
        Map<String,Object> params  = new HashMap<>(3);
        params.put("name",role.getName());
        params.put("description",role.getDescription());
        params.put("role_id",role.getRoleId());

        return roleRepository.updateByRoleId(params);
    }

    @Override
    public List<Permission> findPermissionsByRoleId(String roleId) {
        return permissionRepository.findByRoleId(roleId);
    }

    @Override
    public ResponseVo addAssignPermission(String roleId, List<String> permissionIdsList) {
        try{
            //1.先删除之前角色绑定的权限
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermissionMapper.delete(rolePermission);
            //2.重新绑定权限并保存
            for(String permissionId : permissionIdsList){
                rolePermission.setId(null);
                rolePermission.setPermissionId(permissionId);
                rolePermissionMapper.insert(rolePermission);
            }
            return ResultUtil.success("分配权限成功");
        }catch(Exception e){
            return ResultUtil.error("分配权限失败");
        }
    }

    @Override
    public List<User> findByRoleId(String roleId) {
        return userMapper.findByRoleId(roleId);
    }

    @Override
    public List<User> findByRoleIds(List<String> roleIds) {
        return userMapper.findByRoleIds(roleIds);
    }
}
